//
//  TenantCertificationViewController.m
//  UcarTest
//
//  Created by 陈曦 on 16/3/9.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "TenantCertificationViewController.h"
#import "AFNetworking.h"
#import "Define.h"  
#import "NetRequestGroup.h"
#import "Access_token.h"
#import "MainPageViewController.h"
#import "DIYMenuButton.h"
#import "IdCertificationViewController.h"
#import "DrivingLicenseCertificationViewController.h"
#import "MBProgressHUD.h"

@interface TenantCertificationViewController ()

@property (nonatomic, strong) DIYMenuButton *identitycardBtn;//身份证认证

@property (nonatomic, strong) DIYMenuButton *driverlicenseBtn;//驾驶证认证

@end

@implementation TenantCertificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //导航条
    self.navigationController.navigationBarHidden = NO;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"租客认证";
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.barTintColor = mainColor;
    self.view.backgroundColor = backgroundMainColor;
    

    //重定义返回键
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(0, 0, 22, 22);
    [back setImage:[UIImage imageNamed:@"iconfont-fanhui-white"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backTpMainView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backBar;

    //网络解析
    [self NetworkRequests];
    

}

#pragma mark 网络请求
- (void) NetworkRequests{
    
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    Access_token *useAccess = [Access_token shareInstance];
    
    //传入的参数
    NSDictionary *para = @{
                           @"type" : @"1",
                           @"access_token" : useAccess.access_token
                           };
    //你的接口地址
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName,kNetTenantCertification];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [HUD hideAnimated:YES];

        NSDictionary *dic = responseObject;
        
        
        NSDictionary *info = dic[@"info"];
        
        self.identity_upload_type = info[@"identity_upload_type"];//身份证上传类型
        
        self.identity_upload_is = info[@"identity_upload_is"];//身份证是否可以上传
        
        self.identity_id = info[@"identity_id"];//身份证id,返回给下个接口
        
        self.drive_upload_type = info[@"drive_upload_type"];//驾照上传类型
        
        self.drive_upload_is = info[@"drive_upload_is"];//驾驶证是否可以上传
        
        self.drive_id = info[@"drive_id"];//驾照id,返回给下个接口
        
        //添加视图
        [self additionView];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [HUD hideAnimated:YES];
        
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
        
    }];
    
}

//加载视图
- (void) additionView {
    
    //身份证认证
    self.identitycardBtn = [DIYMenuButton buttonWithType:UIButtonTypeCustom];
    self.identitycardBtn.backgroundColor = [UIColor whiteColor];
    self.identitycardBtn.frame = CGRectMake(0, 84, kScreenWidth, 50);
    self.identitycardBtn.nameplateImageView.frame = CGRectMake(15, 16.8, 18.5, 16.4);
    self.identitycardBtn.functionLabel.text = @"身份证认证";
    self.identitycardBtn.functionLabel.frame = CGRectMake(55, 50 / 4, kScreenWidth / 2, 25);
    if ([self.identity_upload_is isEqualToString:@"0"]) {
        
        self.identitycardBtn.nameplateImageView.image = [UIImage imageNamed:@"icon_id_gary"];
        self.identitycardBtn.functionLabel.textColor = [UIColor colorWithRed:164 / 255.0 green:164 / 255.0 blue:164 / 255.0 alpha:1];
        
    } else if([self.identity_upload_is isEqualToString:@"1"]){
        
        self.identitycardBtn.nameplateImageView.image = [UIImage imageNamed:@"icon_id"];
        self.identitycardBtn.functionLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
        [self.identitycardBtn addTarget:self action:@selector(pushViewControllerToID) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:self.identitycardBtn];
    
    
    UIImageView *grayBackIV2 = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 20, 15, 10, 20)];
    grayBackIV2.image = [UIImage imageNamed:@"iconfont-fanhui-gary"];
    [self.identitycardBtn addSubview:grayBackIV2];
    
    UIView *demarcationLine3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];//分界线
    demarcationLine3.backgroundColor = lineColor;
    [self.identitycardBtn addSubview:demarcationLine3];
    
    UIView *demarcationLine4 = [[UIView alloc] initWithFrame:CGRectMake(10, 49.5, kScreenWidth - 10, 0.5)];//分界线
    demarcationLine4.backgroundColor = lineColor;
    [self.identitycardBtn addSubview:demarcationLine4];

    //驾驶证认证
    self.driverlicenseBtn = [DIYMenuButton buttonWithType:UIButtonTypeCustom];
    self.driverlicenseBtn.frame = CGRectMake(0, 134, kScreenWidth, 50);
    self.driverlicenseBtn.nameplateImageView.frame = CGRectMake(15, 16.8, 18.5, 16.4);
    self.driverlicenseBtn.backgroundColor = [UIColor whiteColor];
    self.driverlicenseBtn.functionLabel.text = @"驾照认证";
    self.driverlicenseBtn.functionLabel.frame = CGRectMake(55, 50 / 4, kScreenWidth / 2, 25);
    if ([self.drive_upload_is isEqualToString:@"0"]) {
        
        self.driverlicenseBtn.nameplateImageView.image = [UIImage imageNamed:@"icon_cardid_gary"];
        self.driverlicenseBtn.functionLabel.textColor = [UIColor colorWithRed:164 / 255.0 green:164 / 255.0 blue:164 / 255.0 alpha:1];
        
    } else if([self.drive_upload_is isEqualToString:@"1"]){
        
        self.driverlicenseBtn.nameplateImageView.image = [UIImage imageNamed:@"icon_cardid"];
        self.driverlicenseBtn.functionLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
        [self.driverlicenseBtn addTarget:self action:@selector(pushViewControllerToDriver) forControlEvents:UIControlEventTouchUpInside];
    }

    [self.view addSubview:self.driverlicenseBtn];
    
    UIImageView *grayBackIV0 = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 20, 15, 10, 20)];
    grayBackIV0.image = [UIImage imageNamed:@"iconfont-fanhui-gary"];
    [self.driverlicenseBtn addSubview:grayBackIV0];
    
    
    UIView *demarcationLine2 = [[UIView alloc] initWithFrame:CGRectMake(0, 49.5, kScreenWidth, 0.5)];//分界线
    demarcationLine2.backgroundColor = lineColor;
    [self.driverlicenseBtn addSubview:demarcationLine2];

}

#pragma mark 身份证认证
- (void) pushViewControllerToID {

    IdCertificationViewController *idCertification = [[IdCertificationViewController alloc] init];
    
    idCertification.identity_upload_type = self.identity_upload_type;
    
    idCertification.identity_id = self.identity_id;
    
    [self.navigationController pushViewController:idCertification animated:nil];

}

#pragma mark 驾驶证认证
- (void) pushViewControllerToDriver {

    DrivingLicenseCertificationViewController *driverCertification = [[DrivingLicenseCertificationViewController alloc] init];
    
    driverCertification.drive_id = self.drive_id;
    
    driverCertification.drive_upload_type = self.drive_upload_type;
    
    [self.navigationController pushViewController:driverCertification animated:nil];


}








//返回主页
- (void)backTpMainView{
        
    MainPageViewController *mainVC = [[MainPageViewController alloc] init];
    
    [self.navigationController pushViewController:mainVC animated:nil];
    
    
}


@end
